use std::{cell::RefCell, rc::Rc, string};

use deno_core::{op2, v8, OpState};
use QcCore::scene_system::scene::Scene;
use QcUI::{component::{Button, Canvas}, core::uiBind::JsUiBind};

use crate::utils::GoExt;

/// 获取组件是否激活
#[op2]
pub fn opGetWidgetActive<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    active: bool,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponent::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get(index) {
                    let widget = node.get_widget();

                    return serde_v8::to_v8(scope, widget.active).unwrap();
                }
            }
        }
    }

    v8::undefined(scope).into()
}

/// 设置组件是否激活
#[op2]
pub fn opSetWidgetActive<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    active: bool,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponentMut::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get_mut(index) {
                    let widget = node.get_widget_mut();

                    widget.active = active;
                }
            }
        }
    }

    v8::undefined(scope).into()
}

/// 获取组件宽度
#[op2]
pub fn opGetWidgetWidth<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    active: bool,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponent::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get(index) {
                    let widget = node.get_widget();

                    return serde_v8::to_v8(scope, widget.width).unwrap();
                }
            }
        }
    }

    v8::undefined(scope).into()
}

/// 设置组件宽度
#[op2]
pub fn opSetWidgetWidth<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    width: f32,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponentMut::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get_mut(index) {
                    let widget = node.get_widget_mut();

                    widget.width = width;
                }
            }
        }
    }

    v8::undefined(scope).into()
}

/// 获取组件高度
#[op2]
pub fn opGetWidgetHeight<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    active: bool,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponent::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get(index) {
                    let widget = node.get_widget();

                    return serde_v8::to_v8(scope, widget.height).unwrap();
                }
            }
        }
    }

    v8::undefined(scope).into()
}

/// 设置组件高度
#[op2]
pub fn opSetWidgetHeight<'a>(
    state: &mut OpState,
    scope: &mut v8::HandleScope<'a>,
    this: v8::Local<v8::Object>,
    height: f32,
) -> v8::Local<'a, v8::Value> {
    let scene = state.borrow_mut::<*mut Scene>();
    let scene = unsafe { &mut **scene };

    if let Some(index) = scene.get_main_canvas() {
        if let Some(canvas) = scene[index].getComponentMut::<Canvas>() {
            if let Some(index) = GoExt::get_uuid(scope, this) {
                if let Some(node) = canvas.get_mut(index) {
                    let widget = node.get_widget_mut();

                    widget.height = height;
                }
            }
        }
    }

    v8::undefined(scope).into()
}

